/**
 * 
 */
package offer.algorithm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhang 字符串的所有排列
 */
public class StringRange28 {

	public static List<String> allRange(String src) {
		if (src == null || src.trim().equals("")) {
			return null;
		}
		List<String> list = new ArrayList<>();
		char[] charArray = src.toCharArray();
		allRange(charArray, 0, charArray.length, list);
		Collections.sort(list);
		return list;
	}

	public static void allRange(char[] src, int start, int end, List<String> list) {
		if (start == end && !list.contains(String.valueOf(src))) {
			list.add(String.valueOf(src));
		} else {
			for (int i = start; i < end; i++) {
				if (i != start && src[i] == src[start]) {
					continue;
				}
				char temp = src[start];
				src[start] = src[i];
				src[i] = temp;
				allRange(src, start + 1, end, list);
				temp = src[start];
				src[start] = src[i];
				src[i] = temp;
			}
		}
	}

	public static void main(String[] args) {
		List<String> list = allRange("abc");
		System.out.println(list);

	}

}
